package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.common.constant.Constant;
import com.pzhu.model.entity.Thali;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.ThaliService;
import com.pzhu.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author : zqh
 * @Date : 2023/12/25 0025 下午 4:14
 * @Version: 1.0
 */
@Api(tags = "套餐后台接口")
@RestController
@RequestMapping("/thali")
public class ThaliController {

    @Autowired
    private ThaliService thaliService;

    @Autowired
    private RedisUtil redisUtil;


    @ApiOperation("查询分类信息")
    @GetMapping("/list")
    public ResponseResult list(){
        return thaliService.listBuildTree();
    }

    @ApiOperation("删除信息")
    @GetMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Long id){
        int count = thaliService.count(new LambdaQueryWrapper<Thali>().eq(Thali::getParentId, id));
        if(count>0){
            return ResponseResult.error("请先删除子菜单！");
        }
        thaliService.removeById(id);
        redisUtil.removeByPrex(Constant.AUTHORITY_KEY);
        return ResponseResult.ok();
    }

    @ApiOperation("分类详情页")
    @GetMapping("/detail/{id}")
    @ApiImplicitParam(name = "id", value = "套餐ID", required = true)
    public ResponseResult detail(@PathVariable(value = "id") Long id){
        return thaliService.detail(id);
    }
}
